'use strict'
const fs = require('fs');

//查找所有过滤文件
function getFliterFlies(path) {
    path = path || './controllers';
    let flies = fs.readdirSync(path);
    //console.log(flies);
    return flies.filter(item => {
        return item.endsWith('.js') && item !== 'index.js';
    });
}

//注册路由文件
function registerRouter(flies, router) {
    flies.forEach(element => {
        let tmpModule = require('../controllers/' + element.replace('.js', ''));
        console.log(tmpModule);

        for (let item in tmpModule) {
            let tmpArr = item.split(' ');

            let method = tmpArr[0];
            let url = tmpArr[1];
            let fn = tmpModule[item];

            if (method === 'get') {
                router.get(url, fn);
            } else if (method === 'post') {
                router.post(url, fn);
            } else if (method === 'put') {
                router.put(url, fn);
            } else if (method === 'delete') {
                router.delete(url, fn);
            }

        }
    });
}

module.exports = {
    getFliterFlies, registerRouter
}